package zhaoq_hero.superapp.fragments;


import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import zhaoq_hero.superapp.R;
import zhaoq_hero.superapp.providers.DataContract;

/**
 * A simple {@link Fragment} subclass.
 */
public class HistoryFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {

    private SimpleCursorAdapter adapter;


    private View ret;
    private Bundle args;
    private CursorLoader cursorLoader;
    private ListView listView;

    public HistoryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ret = inflater.inflate(R.layout.fragment_history, container, false);

        listView = (ListView) ret.findViewById(R.id.history_list_view);

        if (listView != null) {

            adapter = new SimpleCursorAdapter(
                    getActivity(),
                    android.R.layout.simple_list_item_1,
                    null,  //Cursor 数据
                    new String[]{DataContract.History.URL},
                    new int[]{android.R.id.text1},
                    CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER  //内容观察者
            );

            listView.setAdapter(adapter);

            //Loader初始化
            LoaderManager loaderManager = getLoaderManager();

            args = new Bundle();

            args.putString("action","sayHello");

            loaderManager.initLoader(
                    998,
                    args,
                    this
                    );
        }


        listView.setOnItemClickListener(this);

        return ret;
    }

    //刷新数据
    public void refreshHistory(){

        LoaderManager loaderManager = getLoaderManager();

        loaderManager.restartLoader(
                998,
                null,
                this);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        cursorLoader =null;

        switch(id){

            case 998:
                cursorLoader =
                        new CursorLoader(
                                getActivity(),
                                DataContract.History.CONTENT_URI,
                                null,
                                null,
                                null,
                                null);

        }

        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

        /**
         * 使用changeCursor（Cursor）,因为旧的cursor在方法内部关闭了
         */
        adapter.changeCursor(data); //swapCursor没有关闭cursor

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

        adapter.changeCursor(null);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ContentResolver resolver = getActivity().getContentResolver();

        ContentValues values = new ContentValues();

        values.put(DataContract.History.URL,"http://www.baidu.com");

        /**
         * 添加记录  并返回添加记录的访问 Uri
         */
        Cursor query = resolver.query(DataContract.History.CONTENT_URI,
                new String[]{"url"},
                "_id =" + position,
                null,
                null
        );

        while(query.moveToNext())
        {
            int anInt = query.getInt(0);
            String url = query.getString(1);


            Log.i("info",id+"======"+url);
        }


    }
}
